package com.system.entity;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import com.system.main.SystemServer;

public class IFile {

	private File file;
	
	private String type;
	
	private String hasChild;
	
	private String path;
	
	/**
	 * lastModifiedTime 最后更新时间
	 */
	private String lmTime;
	
	private String name = "";

	
	
	public void setLmTime() {
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		date.setTime(file.lastModified());
		lmTime = sdf.format(date);
	}


	/**
	 * 树节点显示的内容
	 * @return
	 */
	public String getText() {
		if(file.getName().length()>0){
			name = file.getName();
		}
		return name;
	}

	public IFile(File file) {
		this.file = file;
		path = file.getAbsolutePath();
		setLmTime();
		String name = file.getName();
		if(name.length()==0){
			this.name = "本地磁盘 "+file.getAbsolutePath();
			type ="root";
		}else{
			if(file.isDirectory()){
				type = "dir";
				hasChild = "true";
			}else{
				hasChild = "false";
				int index = name.lastIndexOf(".");
				if(index == -1 || index == (name.length()-1)){
					 type="unknown";		
				}else{
					type = name.substring(index+1);
				}	
			}
		}
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	/**
	 * @return the isFile
	 */
	public String getHasChild() {
		return hasChild;
	}


	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}
	
	public String getUserObj() throws Exception{
		JSONObject obj =new JSONObject();
		obj.put("id", path);
		obj.put("lmTime",lmTime);
		obj.put("type", type);
		return obj.toString();
	}
	
	/**
	 * 设置成相对地址，前台会自动加上contextPath
	 * @return
	 */
	public String getIconPath(){
		String iconPath =SystemServer.getInstance().getIconPath(type);
		return iconPath;
	}
}
